package com.avic.rong.adapter.show;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avic.rong.MainActivity;
import com.avic.rong.R;
import com.avic.rong.activity.ArticleDetailActivity;
import com.avic.rong.activity.VideoDetailActivity;
import com.avic.rong.activity.login.LoginPhoneActivity;
import com.avic.rong.api.collect.DeleteCollectApi;
import com.avic.rong.api.collect.ZanCancelApi;
import com.avic.rong.bean.show_bean.EsInfoBean;
import com.avic.rong.bean.show_bean.SjbzListBean;
import com.avic.rong.questionOrAnswer.activity.PersonalHomeActivity;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.DateUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ToastTools;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 关注
 * Creater: Created by wangyz on 2017/1/10.
 * Email: 936804097@qq.com
 */

public class SjbzAdapter extends RecyclerView.Adapter<SjbzAdapter.SjbzViewHolder> {

    public static final String TAG = "1";
    private Context mContext;
    private List<SjbzListBean> data = new ArrayList<>();
    private List<EsInfoBean> esBean;
    private String logo;
    private DeleteCollectApi deleteCollectApi;
    private ZanCancelApi zanCancelApi;
    private String mTitle;

    public void setmTitle(String mTitle) {
        this.mTitle = mTitle;
    }

    public SjbzAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<SjbzListBean> list) {
        this.data = list;
        notifyDataSetChanged();
    }

    public void setEsData(List<EsInfoBean> esData) {
        esBean = esData;
        notifyDataSetChanged();
        deleteCollectApi = new DeleteCollectApi((MainActivity) mContext, deleteCollectListener);
        zanCancelApi = new ZanCancelApi((MainActivity) mContext, httpOnNextListener);
    }

    @Override
    public SjbzAdapter.SjbzViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_sjbz_item, parent, false);
        return new SjbzViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SjbzAdapter.SjbzViewHolder holder, final int position) {
        if (data != null && data.size() > 0) {
            SjbzListBean datas = data.get(position);
            holder.datas = datas;
            String coverImgOne = datas.getCoverImgOne();
            String coverImgtwo = datas.getCoverImgTwo();
            String img = datas.getContImg();
            if (!TextUtils.isEmpty(datas.getPublicTime()))
                holder.tv_time.setText(datas.getPublicTime().split(" ")[0]);
            holder.tv_title.setText(datas.getAuthor());
            holder.tv_authenInfo.setText(datas.getAuthenInfo());
            holder.tv_content1.setText(datas.getTitle());
            holder.tv_content.setText(datas.getIntro());
            holder.tv_shoucangsize.setText(datas.getFavouritesNum() + "");
            holder.tv_dianzansize.setText(datas.getThumb() + "");
            holder.line_threeSrc.setVisibility(View.GONE);
            holder.line_oneSrc.setVisibility(View.GONE);
            holder.line_video.setVisibility(View.GONE);
            holder.iv_vip.setVisibility(View.GONE);
            if (1 == datas.getIsV()) {
                holder.iv_vip.setVisibility(View.VISIBLE);
            }
            if (5 == datas.getTypeFlg()) {
                holder.line_video.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(datas.getContImg()).error(R.mipmap.doc_video_default).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.videoImg);
                holder.duraction.setText(datas.getDuraction());
            } else if (!TextUtils.isEmpty(coverImgOne) && !TextUtils.isEmpty(coverImgtwo)) {
                holder.line_threeSrc.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(datas.getContImg()).error(R.mipmap.doc_default).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.three_oneimage);
                Glide.with(mContext).load(datas.getCoverImgOne()).error(R.mipmap.doc_default).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.three_twoimage);
                Glide.with(mContext).load(datas.getCoverImgTwo()).error(R.mipmap.doc_default).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.three_threeimage);
            } else {
                holder.line_oneSrc.setVisibility(View.VISIBLE);
                if (TextUtils.isEmpty(img))
                    holder.one_imageview.setVisibility(View.GONE);
                Glide.with(mContext).load(datas.getContImg()).error(R.mipmap.doc_default).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.one_imageview);
            }

            holder.dianzan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (SharedPreferencesUtil.isLogin()) {
                        for (int k = 0; k < esBean.size(); k++) {
                            EsInfoBean esInfoBean = esBean.get(k);
                            int cont_id = esBean.get(k).getCont_id();
                            int contId = data.get(position).getContId();
                            if (cont_id == contId) {
                                int isThumb = esInfoBean.getIsThumb();
                                if (isThumb != 1) {
                                    esInfoBean.setIsThumb(1);
                                    Glide.with(mContext).load(R.mipmap.orangezan2x).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.iv_dianzan);
                                    int num = datas.getThumb() + 1;
                                    datas.setThumb(num);
                                    ToastTools.showPublicToast("点赞成功");
                                    String logintoken = SharedPreferencesUtil.getInstance(mContext).getLogintoken();
                                    zanCancelApi.setArgument(contId, 1, logintoken);
                                    HttpManager.getInstance().doHttpDeal(zanCancelApi);
                                    notifyDataSetChanged();
                                    return;
                                } else {
                                    esInfoBean.setIsThumb(0);
                                    Glide.with(mContext).load(R.mipmap.whitezan2x).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.iv_dianzan);
                                    int num = datas.getThumb() - 1;
                                    datas.setThumb(num);
                                    ToastTools.showPublicToast("已取消点赞");
                                    String logintoken = SharedPreferencesUtil.getInstance(mContext).getLogintoken();
                                    zanCancelApi.setArgument(contId, 1, logintoken);
                                    HttpManager.getInstance().doHttpDeal(zanCancelApi);
                                    notifyDataSetChanged();
                                    return;
                                }

                            }
                        }


                    } else {
                        ToastTools.showToast("请先登录");

                        mContext.startActivity(new Intent(mContext, LoginPhoneActivity.class));
                    }

                }
            });

            holder.shoucang.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (SharedPreferencesUtil.isLogin()) {

                        for (int k = 0; k < esBean.size(); k++) {
                            EsInfoBean esInfoBean = esBean.get(k);
                            int cont_id = esBean.get(k).getCont_id();
                            int contId = data.get(position).getContId();
                            if (cont_id == contId) {
                                int isFavorites = esInfoBean.getIsFavorites();
                                if (isFavorites != 1) {
                                    esInfoBean.setIsFavorites(1);
                                    Glide.with(mContext).load(R.mipmap.yellowstar2x).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.iv_shoucang);
                                    int num = datas.getFavouritesNum();
                                    num++;
                                    datas.setFavouritesNum(num);
                                    ToastTools.showPublicToast("收藏成功");
                                    String logintoken = SharedPreferencesUtil.getInstance(mContext).getLogintoken();
                                    deleteCollectApi.setArgument(logintoken, 12, contId);
                                    HttpManager.getInstance().doHttpDeal(deleteCollectApi);
                                    notifyDataSetChanged();
                                    return;
                                } else {
                                    esInfoBean.setIsFavorites(0);
                                    Glide.with(mContext).load(R.mipmap.whitestar2x).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.iv_shoucang);
                                    int num = datas.getFavouritesNum();
                                    num--;
                                    datas.setFavouritesNum(num);
                                    ToastTools.showPublicToast("已取消收藏");
                                    String logintoken = SharedPreferencesUtil.getInstance(mContext).getLogintoken();
                                    deleteCollectApi.setArgument(logintoken, 12, contId);
                                    HttpManager.getInstance().doHttpDeal(deleteCollectApi);
                                    notifyDataSetChanged();
                                    return;
                                }

                            }
                        }

                    } else {
                        ToastTools.showToast("请先登录");

                        mContext.startActivity(new Intent(mContext, LoginPhoneActivity.class));
                    }
                }
            });

            holder.share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myClickListener.onItemClick(position);
                }
            });

            holder.iv_icon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (DateUtils.isFastDoubleClick()) return;

                    Intent intent = new Intent(mContext, PersonalHomeActivity.class);
                    intent.putExtra("homeUserID", datas.getAdminId() + "");
                    mContext.startActivity(intent);
                }
            });

            for (int k = 0; k < esBean.size(); k++) {
                EsInfoBean esInfoBean = esBean.get(k);
                int cont_id = esBean.get(k).getCont_id();
                int contId = data.get(position).getContId();
                if (cont_id == contId) {
                    int isFavorites = esInfoBean.getIsFavorites();
                    int isThumb = esInfoBean.getIsThumb();
                    int view_num = esInfoBean.getView_num();
                    logo = esInfoBean.getLogo();
                    Glide.with(mContext).load(logo).error(R.mipmap.deafult_image_header).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(holder.iv_icon);
                    if (isThumb == 1) {
                        Glide.with(mContext).load(R.mipmap.orangezan2x).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.iv_dianzan);
                    } else {
                        Glide.with(mContext).load(R.mipmap.whitezan2x).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.iv_dianzan);
                    }


                    if (isFavorites == 1) {
                        Glide.with(mContext).load(R.mipmap.yellowstar2x).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.iv_shoucang);
                    } else {
                        Glide.with(mContext).load(R.mipmap.whitestar2x).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.iv_shoucang);
                    }
                    return;
                }
            }

        }
    }


    HttpOnNextListener<BaseResultEntity> deleteCollectListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
//                if (isFavorites) {
//                    ToastTools.showPublicToast("收藏成功");
//                } else {
//                    ToastTools.showPublicToast("已取消收藏");
//                }
            } else {
                ToastTools.showPublicToast("收藏失败");
            }
        }
    };

    HttpOnNextListener<BaseResultEntity> httpOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals("200")) {
//                Toast.makeText(context,"操作成功",Toast.LENGTH_LONG).show();
            } else {
//                Toast.makeText(context,"操作失败",Toast.LENGTH_LONG).show();
            }
        }
    };

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    MyClickListener myClickListener;

    public interface MyClickListener {
        void onItemClick(int position);
    }

    public void setItemClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    class SjbzViewHolder extends RecyclerView.ViewHolder {

        SjbzListBean datas;
        ImageView iv_icon, iv_vip, one_imageview, three_twoimage, three_oneimage, three_threeimage,
                iv_shoucang, iv_dianzan, videoImg;
        TextView tv_title, tv_time, tv_content1, tv_content, tv_shoucangsize, tv_dianzansize,
                duraction, tv_authenInfo;
        LinearLayout line_oneSrc, line_threeSrc, shoucang, dianzan, share;
        RelativeLayout line_video;

        public SjbzViewHolder(View itemView) {
            super(itemView);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                            int contId = data.get(position).getId();
//                            for (int i = 0; i < esInfoBeans.size(); i++) {
//                                if (contId == esInfoBeans.get(i).getCont_id()) {
//                                    view_num = esInfoBeans.get(i).getView_num();
//                                    view_num++;
//                                    esInfoBeans.get(i).setView_num(view_num);
//                                    notifyDataSetChanged();
//                                }
//                            }
                    if (5 == datas.getTypeFlg()) {
                        Intent intent = new Intent(mContext, VideoDetailActivity.class);
                        intent.putExtra("contId", datas.getContId());
                        mContext.startActivity(intent);
                    } else {
                        Intent intent = new Intent(mContext, ArticleDetailActivity.class);
                        intent.putExtra("contId", datas.getContId());
                        intent.putExtra(ArticleDetailActivity.ARG_TIP, mTitle);
                        mContext.startActivity(intent);
                    }

                }
            });

            iv_icon = itemView.findViewById(R.id.iv_icon);
            iv_vip = itemView.findViewById(R.id.iv_vip);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_authenInfo = itemView.findViewById(R.id.tv_authenInfo);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_content1 = itemView.findViewById(R.id.tv_content1);
            one_imageview = itemView.findViewById(R.id.one_imageview);
            tv_content = itemView.findViewById(R.id.tv_content);
            three_oneimage = itemView.findViewById(R.id.three_oneimage);
            three_twoimage = itemView.findViewById(R.id.three_twoimage);
            three_threeimage = itemView.findViewById(R.id.three_threeimage);
            line_oneSrc = itemView.findViewById(R.id.line_oneSrc);
            line_threeSrc = itemView.findViewById(R.id.line_threeSrc);
            iv_shoucang = itemView.findViewById(R.id.iv_shoucang);
            tv_shoucangsize = itemView.findViewById(R.id.tv_shoucangsize);
            iv_dianzan = itemView.findViewById(R.id.iv_dianzan);
            tv_dianzansize = itemView.findViewById(R.id.tv_dianzansize);
            shoucang = itemView.findViewById(R.id.shoucang);
            dianzan = itemView.findViewById(R.id.dianzan);
            share = itemView.findViewById(R.id.share);
            line_video = itemView.findViewById(R.id.line_video);
            videoImg = itemView.findViewById(R.id.videoImg);
            duraction = itemView.findViewById(R.id.duraction);


        }
    }
}
